package com.baby.growth.service;

import com.baby.growth.dao.BabyInputDAO;
import com.baby.growth.module.BabyInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BabyInputService {
    @Autowired
    BabyInputDAO babyInputDAO;

    public List<BabyInput> list() {
        return babyInputDAO.findAll(Sort.by(Sort.Direction.ASC, "date"));
    }

    public List<BabyInput> listToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date todayZero = calendar.getTime();

        return babyInputDAO.findByDateGreaterThanEqual(todayZero, Sort.by(Sort.Direction.ASC, "date"));
    }

    public void addOrUpdate(BabyInput babyInput) {
        babyInputDAO.save(babyInput);
    }

    public void deleteById(int id) {
        babyInputDAO.deleteById(id);
    }
}
